/* $Id: UninstallActivity.java 86 2007-12-20 22:56:41Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.PackageManager.ApplicationInfo;
import android.os.Bundle;
import android.view.Menu;

import com.bitsetters.android.common.BulletedText;
import com.bitsetters.android.common.BulletedTextListAdapter;
import com.bitsetters.undroid.common.CommonMenu;
import com.bitsetters.undroid.common.Footer;
import com.bitsetters.undroid.helpers.DbHelper;

/**
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class UninstallActivity extends ListActivity {
    
    public static final String TAG = "UNDROID[UninstallActivity]";
    private DbHelper dbHelper;
    
    /**
     * 
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        dbHelper = new DbHelper(this);
       
        setContentView(R.layout.list);
        
        List<BulletedText> items = new ArrayList<BulletedText>();
        items = installedApplications();
        BulletedTextListAdapter btla = new BulletedTextListAdapter(this);
        btla.setListItems(items);
        setListAdapter(btla);

        Footer.setup(this);
    }
    
    /**
     * 
     */
    private List<BulletedText> installedApplications() {
        List<BulletedText> items = new ArrayList<BulletedText>();
        
        List<ApplicationInfo> packages = getPackageManager().getInstalledApplications(0);
        
        for(int i=0; i< packages.size(); i++) {
            String label = packages.get(i).packageName;
            
            try {
                label = getPackageManager().getApplicationLabel(packages.get(i)).toString();
            } catch (Exception e) {
                
            }
            
            
            // Don't list system applications
            if(!packages.get(i).sourceDir.contains("/system/")) {
                BulletedText bt = new BulletedText(
                    label,
                    getPackageManager().getApplicationIcon(packages.get(i)));
                items.add(bt);
            }
        }
        
        return items;
    }
    
    /**
     * 
     */
    @Override
    protected void onPause() {
        super.onPause();
        dbHelper.close();
        dbHelper = null;
    }
    
    /**
     * 
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (dbHelper == null) {
            dbHelper = new DbHelper(this);
        }
    }
    
    /**
     * 
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return CommonMenu.createMenu(this, menu);
    }

    /**
     * 
     */
    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        return CommonMenu.handleOption(item);
    }
}
